/*
 * Config.java
 *
 * Created on May 6, 2010, 2:21 PM
 */
package ConnectDB;

import GUI.frmLogin;
import Validate_Connect.CheckForm;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * 
 * @author  Shinichi
 */
public class frmConfig extends javax.swing.JFrame {

    /** Creates new form Config */
    public frmConfig() {
        LookAndFeelWindow.LookAndFeelWindow.LookAndFeelWindow();
        initComponents();
        getRootPane().setDefaultButton(_btnconfig);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDesktopPane1 = new javax.swing.JDesktopPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        _txtservername = new javax.swing.JTextField();
        _txtusername = new javax.swing.JTextField();
        _txtport = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        _btnconfig = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        _txtdatabasename = new javax.swing.JTextField();
        _pwfpassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Configs");

        jDesktopPane1.setBackground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Server Name:");
        jLabel1.setBounds(30, 80, 70, 14);
        jDesktopPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Username:");
        jLabel2.setBounds(30, 110, 60, 14);
        jDesktopPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Password:");
        jLabel3.setBounds(30, 140, 60, 14);
        jDesktopPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Port:");
        jLabel4.setBounds(30, 170, 24, 14);
        jDesktopPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        _txtservername.setBounds(120, 80, 160, 19);
        jDesktopPane1.add(_txtservername, javax.swing.JLayeredPane.DEFAULT_LAYER);
        _txtusername.setBounds(120, 110, 160, 19);
        jDesktopPane1.add(_txtusername, javax.swing.JLayeredPane.DEFAULT_LAYER);
        _txtport.setBounds(120, 170, 160, 19);
        jDesktopPane1.add(_txtport, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabel5.setForeground(new java.awt.Color(0, 0, 204));
        jLabel5.setText("Configs Server");
        jLabel5.setBounds(80, 10, 160, 40);
        jDesktopPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        _btnconfig.setText("Config");
        _btnconfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btnconfigActionPerformed(evt);
            }
        });
        _btnconfig.setBounds(120, 240, 73, 23);
        jDesktopPane1.add(_btnconfig, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Database Name:");
        jLabel6.setBounds(30, 200, 80, 14);
        jDesktopPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        _txtdatabasename.setText("AITSM");
        _txtdatabasename.setBounds(120, 200, 160, 19);
        jDesktopPane1.add(_txtdatabasename, javax.swing.JLayeredPane.DEFAULT_LAYER);
        _pwfpassword.setBounds(120, 140, 160, 23);
        jDesktopPane1.add(_pwfpassword, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDesktopPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 322, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jDesktopPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void _btnconfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btnconfigActionPerformed
    if(!check()){
        return;
    }    
    String _serverName = _txtservername.getText();
    String _databaseName = _txtdatabasename.getText();
    String _port = _txtport.getText();
    String _username = _txtusername.getText();
    char[] _chrpass = _pwfpassword.getPassword();
    String _password = new String(_chrpass);

    Properties _pro = new Properties();
    File file = new File("configs.properties");
    try {
        if (!file.exists()) {
            file.createNewFile();
            _pro.setProperty("serverName", _serverName);
            _pro.setProperty("databaseName", _databaseName);
            _pro.setProperty("port", _port);
            _pro.setProperty("userName", _username);
            _pro.setProperty("password", _password);
            FileOutputStream fos = new FileOutputStream(file);
            _pro.store(fos, "");
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    JOptionPane.showMessageDialog(null, "Configs have been saved", "Configs", JOptionPane.INFORMATION_MESSAGE);
    this.dispose();
    frmLogin _frmlogin;
    try {
        _frmlogin = new frmLogin();
        _frmlogin.setLocationRelativeTo(null);
        _frmlogin.setVisible(true);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(frmConfig.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(frmConfig.class.getName()).log(Level.SEVERE, null, ex);
    }


}//GEN-LAST:event__btnconfigActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new frmConfig().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _btnconfig;
    private javax.swing.JPasswordField _pwfpassword;
    private javax.swing.JTextField _txtdatabasename;
    private javax.swing.JTextField _txtport;
    private javax.swing.JTextField _txtservername;
    private javax.swing.JTextField _txtusername;
    private javax.swing.JDesktopPane jDesktopPane1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    // End of variables declaration//GEN-END:variables


    public boolean check()
    {
        char[] _chrpass = _pwfpassword.getPassword();
        String _password = new String(_chrpass);
        if(!CheckForm.isEmpty(_txtservername.getText())){
            JOptionPane.showMessageDialog(null, "Please enter server name", "Warning", JOptionPane.WARNING_MESSAGE);
            _txtservername.requestFocus();
            return false;
        }
        if(!CheckForm.isEmpty(_txtusername.getText())){
            JOptionPane.showMessageDialog(null, "Please enter username server", "Warning", JOptionPane.WARNING_MESSAGE);
            _txtusername.requestFocus();
            return false;
        }
        if(!CheckForm.isEmpty(_password)){
            JOptionPane.showMessageDialog(null, "Please enter password server", "Warning", JOptionPane.WARNING_MESSAGE);
            _pwfpassword.requestFocus();
            return false;
        }
        if(!CheckForm.isEmpty(_txtport.getText())){
            JOptionPane.showMessageDialog(null, "Please enter server port", "Warning", JOptionPane.WARNING_MESSAGE);
            _txtport.requestFocus();
            return false;
        }       
        return true;
    }
}
